#include "userdelegate.h"
#include <QPainter>
#include <QDebug>
#include <QAbstractItemDelegate>
#include <dataview/usermodel.h>
#include <QLinearGradient>

UserDelegate::UserDelegate(QObject* parent):QAbstractItemDelegate(parent)
{

}


void UserDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    //QColor color(Qt::darkGray);
    QColor color(218, 218, 218);
    //QBrush brush()
    QString showName = index.data(UserModel::ROLE_NAME).toString();
    QString permision /*= index.data(UserModel::ROLE_PERMISION).toString()*/;
    int i = index.data(UserModel::ROLE_PERMISION).toInt();
    switch (i) {
    case UserModel::TECH:
        permision = "工程师";
        break;
    case UserModel::ADMIN:
        permision = "管理员";
        break;
    case UserModel::OPERATOR:
        permision = "操作员";
        break;
    default:
        break;
    }

    if(!showName.isEmpty())
    {
        if(option.state&QStyle::State_Selected)
        {
            painter->setBrush(Qt::darkBlue);
        }
        else {//QLinearGradient(0,0,0,1,QColor(255, 255, 255, 255),QColor(134, 187, 237, 255));
            QLinearGradient lg(0,0,0,option.rect.height());
            lg.setColorAt(1,QColor(134,187,237,255));
            lg.setColorAt(0,QColor(255,255,255,255));
            //QBrush brush(lg);
            painter->setBrush(lg);
        }
        painter->drawRect(option.rect);
        painter->setPen(Qt::black);
        painter->setFont(QFont("Ubuntu Bold",30));
        painter->drawText(option.rect,showName,Qt::AlignBottom|Qt::AlignHCenter);
        painter->setPen(Qt::black);
        painter->setFont(QFont("Ubuntu Regula",12));
        painter->drawText(QRect(option.rect.right()-60,option.rect.top(),60,20),permision,Qt::AlignHCenter|Qt::AlignVCenter);
        QPixmap pm;
        QRect rect;
        pm.load(":/res/user1.png");
        rect = QRect(option.rect.left()+40,option.rect.top(),option.rect.width()-80,option.rect.height()-30);
        painter->drawPixmap(rect,pm);

    }
    painter->restore();
}

QSize UserDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//    QSize sz = QAbstractItemDelegate::sizeHint(option,index);
    int h = option.rect.height();
    int w = option.rect.width();
    qDebug()<<h<<w;
    if(index.isValid())
        return QSize(50,105);
}


